import { PrinterComponent } from './../printer.component';
import { NavController } from 'ionic-angular';
import { DeviceManager } from './../../../../common/mpj-deviceCenter/src/deviceManager';
import { MdSnackBar } from '@angular/material';
import { GrowlMessageType, MessageBoxService } from './../../../../component/messageBoxService';
import { PrintComponentService } from './../templates/printcontainer.component';
import { Printer } from './../../../../common/devices/printer/printer';
import { PrinterManager } from './../../../../common/devices/printer/printerManager';
import { Component, OnInit, animate } from '@angular/core';
import { NavigateService } from "../../../../service/navigate.service";
import { guid } from "../../../../common/guid";

@Component({
    selector: 'printer-setting',
    templateUrl: './setprinter.component.html',
    styleUrls: ['./setprinter.component.scss']
})

export class SetPrinterComponent {
    constructor(
        public router: NavigateService,
        private msgSvr: MessageBoxService,
        public snackBar: MdSnackBar,
        public navCtrl: NavController
    ) { }
    printerManager: any = {};
    findPrinters: any = [];
    currentPrinter;
    currentPageSize;
    currentScrollRow;
    currentInstructionSet;
    currentAutoCut = false;

    ionViewWillEnter() {
        this.router.registGoBack(() => {
            let backBtn = document.getElementById("setprinter-back-btn");
            if (backBtn) {
                backBtn.click();
            }
        });
        this.printerManager = DeviceManager.Current.getManagerByType("printerManager");
        console.log("打印管理器：", this.printerManager);
        if (!this.printerManager) {
            return;
        }
        this.findPrinters = this.printerManager.getFindDevices();
        this.currentInstructionSet = this.printerManager.instructionSets[0];
        this.currentPageSize = this.printerManager.selectPageSizes[0];
        this.currentScrollRow = this.printerManager.paperScrollRows[3];
        if (this.findPrinters.length) {
            this.currentPrinter = this.findPrinters[0];
            this.setPrinterParams()
        }
    }

    goBack() {
        // this.router.navigate(["addPrinter"]);
        this.navCtrl.pop({ animate: false });
    }
    /**
     * 根据选择设置打印的参数
     */
    setPrinterParams() {
        this.currentPrinter.instructionSet = this.currentInstructionSet.value;
        this.currentPrinter.paperSize = this.currentPageSize.value;
        this.currentPrinter.paperScrollRows = this.currentScrollRow.value;
        this.currentPrinter.autoCut = this.currentAutoCut;
    }

    //测试打印
    async testPrint() {
        this.setPrinterParams();
        let printParams = {
            token: this.router.Token,
            store: this.router.CurrentStore,
            isTestPrint: true,
            printers: [this.currentPrinter]
        }
        let printTask: any = await PrintComponentService.current.show(printParams);
        let result = await this.printerManager.printHtml(printTask);
        let msg = this.printerManager.errMsg(result.data);
        if (msg) {
            this.msgSvr.Growl(GrowlMessageType.Warning, this.snackBar, msg);
        }
    }

    /**
     * 
     * 添加打印机到打印管理器
     * @memberOf SetPrinterComponent
     */
    addPrinter() {
        this.setPrinterParams();
        this.currentPrinter.id = guid();
        this.printerManager.addDevice(this.currentPrinter);
        // this.router.navigate(["printer"]);
        this.navCtrl.push(PrinterComponent, {}, { animate: false });
    }
}